package org.tao.lightningmq.broker.utils;

/**
 * @Author lht
 * @date 2025/1/3 - 15:58
 * @description:
 */
public class ByteConvertUtil {

    public static byte[] readInPos(byte[] source, int pos, int len) {
        byte[] result = new byte[len];
        System.arraycopy(source, pos, result, 0, len);
        return result;
    }

    public static byte[] intToBytes(int value) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (value & 0xff);
        bytes[1] = (byte) ((value >> 8) & 0xff);
        bytes[2] = (byte) ((value >> 16) & 0xff);
        bytes[3] = (byte) ((value >> 24) & 0xff);
        return bytes;
    }

    public static int bytesToInt(byte[] bytes) {
        int value;
        value = (int) ((bytes[0] & 0xFF)
                | ((bytes[1] & 0xFF) << 8)
                | ((bytes[2] & 0xFF) << 16)
                | ((bytes[3] & 0xFF) << 24));
        return value;
    }

    public static void main(String[] args) {
        int value = 123456;
        byte[] bytes = intToBytes(value);
        System.out.println(bytesToInt(bytes));
        int anInt = bytesToInt(bytes);
        assert anInt == value;
    }

}
